<template>
  <!-- 订单发货页面 -->
  <el-dialog :visible.sync="visible" title="订单发货" :close-on-click-modal="false" :close-on-press-escape="false"  @close="cancellation">
    <div class="flex_columns">
      <div class="flex_columns orderBg2" style="border: none">
        <div style="font-size: 24px;color: #000000">订单信息</div>
        <div class="flex_wrap flex_center flex_between">
          <div class="flex_rows flex_center orderCk">
            <div class="orderTitle">订单号</div>
            <div>{{dataForm.id}}</div>
          </div>
          <div class="flex_rows flex_center orderCk">
            <div class="orderTitle">支付金额</div>
            <div>￥{{formatNumber(dataForm.totalAmount)}}</div>
          </div>
          <div class="flex_rows flex_center orderCk" v-if="dataForm['payDate']">
            <div class="orderTitle">支付时间</div>
            <div>{{dataForm.payDate}}</div>
          </div>
          <div class="flex_rows flex_center orderCk">
            <div class="orderTitle">支付方式</div>
            <div>{{getDictLabel('pay_way',dataForm.payWay)}}</div>
          </div>
          <div class="flex_rows flex_center orderCk" style="width: 100%;" v-if="dataForm['itemDesc']">
            <div class="orderTitle">商品信息</div>
            <div>{{dataForm.itemDesc}}</div>
          </div>
          <div class="flex_rows flex_center orderCk" style="width: 100%;">
            <div class="orderTitle">订单备注</div>
            <div v-if="dataForm.remark">{{dataForm.remark}}</div>
            <div v-else>未填写</div>
          </div>
          <div class="flex_rows flex_center orderCk" v-if="dataForm['receiveName']">
            <div class="orderTitle">收件人姓名</div>
            <div>{{dataForm.receiveName}}</div>
          </div>
          <div class="flex_rows flex_center orderCk" v-if="dataForm['receivePhone']">
            <div class="orderTitle">收件人电话</div>
            <div>{{dataForm.receivePhone}}</div>
          </div>
          <div class="flex_rows flex_center orderCk" style="width: 100%;" v-if="dataForm['receiveProvince']">
            <div class="orderTitle">收件人地址</div>
            <div>{{dataForm.receiveProvince+dataForm.receiveCity+dataForm.receiveCounty+dataForm.receiveAddress}}</div>
          </div>
          <div class="flex_rows flex_center orderCk" style="width: 100%;">
            <div class="orderTitle">自动收货提醒</div>
            <div>物流快递：自动确认收货周期T+10，结算周期T+0。其他发货方式：自动确认收货周期T+2，结算周期T+0。</div>
          </div>
        </div>
      </div>
    </div>
    <div style="padding: 10px 30px;">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-position="left" label-width="120px">
        <el-form-item prop="logisticsType" label="物流模式">
          <ren-radio-group v-model="dataForm.logisticsType" dict-type="logistics_type" placeholder="请选择物流模式"></ren-radio-group>
        </el-form-item>
        <!--      <el-form-item prop="consignorContact" label="寄件人电话" v-if="dataRule.consignorContact[0].required">-->
        <!--        <el-input v-model="dataForm.consignorContact" type="tel" placeholder="请输入寄件人电话" clearable></el-input>-->
        <!--      </el-form-item>-->
        <!--      <el-form-item prop="receiverContact" label="收件人电话" v-if="dataRule.receiverContact[0].required">-->
        <!--        <el-input v-model="dataForm.receiverContact" type="tel" placeholder="请输入收件人电话" clearable></el-input>-->
        <!--      </el-form-item>-->
        <el-form-item prop="expressCompany" label="物流公司" v-if="dataRule.expressCompany[0].required && dataForm.logisticsType=='1'">
          <el-select v-model="dataForm.expressCompany" filterable clearable placeholder="请选择物流公司" @change="changeSelect">
            <el-option v-for="item in expressCompanyList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="expressCompany" label="配送公司" v-if="dataRule.trackingNo[0].required &&dataForm.logisticsType=='2'">
          <el-input v-model="dataForm.expressCompany" placeholder="请输入配送公司名称" clearable></el-input>
        </el-form-item>
        <el-form-item prop="trackingNo" :label="dataForm.logisticsType==='2'?'配送单号':'物流单号' " v-if="dataRule.trackingNo[0].required">
          <el-input v-model="dataForm.trackingNo" placeholder="请输入物流单号" clearable></el-input>
        </el-form-item>
      </el-form>
    </div>

    <template slot="footer">
      <el-button @click="cancellation">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import {formatNumber, getDictDataList, getDictLabel} from "@/utils";

export default {
  data() {
    return {
      visible: false,
      url: '',
      dialogImageUrl: '',
      dialogVisible: false,
      imagelist:{},
      expressCompanyList:[],                //所有物流公司信息
      dataForm: {
        id: '',                         //订单id
        userId:'',                      //用户id
        logisticsType:'',               //物流模式
        itemDesc:'',                    //商品信息，例如：微信红包抱枕*1个，限120个字以内
        consignorContact:'',            //寄件人联系方式
        expressCompany:'',              //物流公司编码，快递公司ID
        expressCompanyName:'',          //物流公司名称
        receiverContact:'',             //收件人联系方式，收件人联系方式为，采用掩码传输，最后4位数字不能打掩码 示例值: `189****1234, 021-****1234, ****1234, 0**2-***1234, 0**2-******23-10, ****123-8008` 值限制: 0 ≤ value ≤ 1024
        trackingNo:'',                  //物流单号，物流快递发货时必填，示例值: 323244567777 字符字节限制: [1, 128]
      }
    }
  },
  watch: {
    'dataForm.logisticsType'(newValue, oldValue){
      this.dataFormChange(newValue)
    }
  },
  computed: {
    dataRule() {
      return {
        logisticsType: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        consignorContact: [
          {required: !(this.dataForm.logisticsType === '3' || this.dataForm.logisticsType === '4'), message: this.$t('validate.required'), trigger: 'blur'}
        ],
        expressCompany: [
          {required: !(this.dataForm.logisticsType === '3' || this.dataForm.logisticsType === '4'), message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiverContact: [
          {required: !(this.dataForm.logisticsType === '3' || this.dataForm.logisticsType === '4'), message: this.$t('validate.required'), trigger: 'blur'}
        ],
        trackingNo: [
          {required: !(this.dataForm.logisticsType === '3' || this.dataForm.logisticsType === '4'), message: this.$t('validate.required'), trigger: 'blur'}
        ],
      }
    }
  },
  mounted() {
    this.dataFormChange()
  },
  methods: {
    getDictLabel,
    formatNumber,
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
          this.userOrderDelivery()
        }
      })
    },
    //监听dataForm.logisticsType变化
    dataFormChange(e) {
      this.dataForm.expressCompanyName = ''
      this.dataForm.expressCompany = ''
      this.dataForm.consignorContact = ''
      this.dataForm.receiverContact = ''
      this.dataForm.trackingNo = ''
      if (this.dataForm.logisticsType==='1'||this.dataForm.logisticsType==='2'){
        this.getShopInfo()
        this.dataForm.receiverContact = this.dataForm.receivePhone
      }
    },
    // 获取公司信息
    getShopInfo() {
      this.$http.get(`/api/shopInfo/getShopInfo`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm.consignorContact = res.data.sendPhone
      }).catch(() => {
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/userOrder/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.dataForm.receiverContact = res.data.receivePhone
        this.dataForm.logisticsType=this.dataForm.logisticsType?this.dataForm.logisticsType:'1'
      }).catch(() => {})
    },
    // 获取运力信息
    userOrderDelivery() {
      this.expressCompanyList = getDictDataList('delivery_list')
      //console.log(this.expressCompanyList)
    },
    //监听下拉
    changeSelect(e){
      this.dataForm.expressCompanyName = this.expressCompanyList[this.expressCompanyList.map(v=>v.dictValue).indexOf(e)].dictLabel
    },
    //取消
    cancellation(){
      this.visible = false
      this.$emit('cancellation')
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        //console.log(this.dataForm)
        if (!valid) {
          return false
        }
        if (this.dataForm.logisticsType==='3'||this.dataForm.logisticsType==='4'){
          this.dataForm.consignorContact = ''
          this.dataForm.expressCompany = ''
          this.dataForm.expressCompanyName = ''
          this.dataForm.receiverContact = ''
          this.dataForm.trackingNo = ''
        }
        if (this.dataForm.logisticsType==='2'){
          this.dataForm.expressCompanyName = this.dataForm.expressCompany
        }
        this.$http['post']('/api/userOrder/uploadShippingInfo', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}
.orderBg2{
  padding: 0 30px;
//border-radius: 10px;
//background-color: #FFFFFF;
  border-bottom: #8a979e solid 1px;
}
.orderTitle{
  color: #979997;
  min-width: 100px;
}
.orderCk{
  margin-top: 20px;
  width: 46%;
}
</style>
